package com.exsun.ex1077.ui.activity.modifypwd

import android.os.Bundle
import com.exsun.ex1077.databinding.ActivityModifyPwdBinding
import com.exsun.ex1077.extend.logout
import com.exsun.ex1077.ui.activity.login.LoginActivity
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.extend.finishToActivity
import com.zhangping.fastjetpack.extend.showToast
import com.zhangping.fastjetpack.extend.view.clickNoRepeat
import kotlinx.android.synthetic.main.activity_modify_pwd.*

/**
 * 描述：修改密码
 * 作者：ZhangPing
 * 创建时间：2022/8/11 10:45
 */
class ModifyPwdActivity: BaseActivity<ModifyPwdViewModel, ActivityModifyPwdBinding>() {

    override fun title() = "修改密码"

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        oldDel.setOnClickListener { mViewModel.oldPwd.set("") }
        newDel.setOnClickListener { mViewModel.newPwd.set("") }
        confirmDel.setOnClickListener { mViewModel.confirmPwd.set("") }

        oldEye.setOnClickListener { mViewModel.showOldPwd.reversal() }
        newEye.setOnClickListener { mViewModel.showNewPwd.reversal() }
        confirmEye.setOnClickListener { mViewModel.showConfirmPwd.reversal() }

        submit.clickNoRepeat {
            mViewModel.apply {
                if (oldPwd.get().isNullOrEmpty()){
                    "请输入原密码".showToast()
                    return@clickNoRepeat
                }
                if (newPwd.get().isNullOrEmpty()){
                    "请输入新密码".showToast()
                    return@clickNoRepeat
                }
                if (confirmPwd.get().isNullOrEmpty()){
                    "请输入确认密码".showToast()
                    return@clickNoRepeat
                }
                if (newPwd.get() != confirmPwd.get()){
                    "两次密码输入不一致".showToast()
                    return@clickNoRepeat
                }
                if (oldPwd.get() == newPwd.get()){
                    "新密码不能于原密码相同".showToast()
                    return@clickNoRepeat
                }
                mViewModel.submitModify()
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.submitResult.observe(this){
            parseState(it, {
                "密码修改成功".showToast()
                logout()
                finishToActivity<LoginActivity>()
            })
        }
    }

}